//
//  VCOfQuestionsAndAnswers.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/1.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfQuestionsAndAnswers.h"
#import "BannerCycleImageView.h"
#import "MJMacro.h"
#import "BaseCollectionView.h"
#import "BaseTableView.h"
#import "CVCellOfChoiceButton.h"
#import "TVCellOfQuestionsAndAnswers.h"
#import "OnePsychologyViewController.h"
#import "BaseNavigationController.h"
#import "RootClassOfQuestionsAndAnswers.h"
#import "DataOfQuestionsAndAnswers.h"
#import "BannerOfQuestionsAndAnswers.h"
#import "AnswerOfQuestion.h"
#import "MsgOfQuestionsAndAnswers.h"
#import "QuestionOfQuestionsAndAnswers.h"
#import "TagsOfQuestion.h"
#import "UserOfQuestion.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "VCOfSpecialistDisabuse.h"
#import "VCOfQuestionsAndAnswersAuslese.h"
#import "MJRefresh.h"
// 问答详情的 VC
#import "VCOfQAADetailedInformation.h"
@interface VCOfQuestionsAndAnswers ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableViewOfQuestionsAndAnswers;
@property (nonatomic, strong) RootClassOfQuestionsAndAnswers *rootClassOfQuestionsAndAnswers;
@end

@implementation VCOfQuestionsAndAnswers

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"问答社区";
    // 数据处理
    [self handleData];
    // 创建子控件
    [self createSubView];
    // 创建提问的button
    UIButton *buttonAsk = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonAsk setTintColor:[UIColor whiteColor]];
    buttonAsk.frame = CGRectMake(SCREEN_WIDTH - 80, SCREEN_HEIGHT - 200, 60, 60);
    buttonAsk.layer.cornerRadius = 30;
    [buttonAsk setTitle:@"✏️提问" forState:UIControlStateNormal];
    [buttonAsk addTarget:self action:@selector(pleaseWait) forControlEvents:UIControlEventTouchUpInside];
    buttonAsk.backgroundColor = [UIColor colorWithRed:0.15 green:0.51 blue:0.93 alpha:0.70];
    [self.view addSubview:buttonAsk];
}
#pragma mark - 收藏中的FM不能上一曲下一曲
- (void)pleaseWait{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"暂未开放" message:@"你可以尝试别的东西" preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}
#pragma mark - 数据处理
- (void)handleData{
    [ZWYNetTool POST:@"http://yiapi.xinli001.com/v2/yi/get-qa-home.json" andBody:@"offset=0&limit=10&sort=reply&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f76b05d4e0e2572d3621fe25def0729b&_version=4.0" andBodyStyle:ZWYBodyString andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"9.26:%@", result);
        _rootClassOfQuestionsAndAnswers = [RootClassOfQuestionsAndAnswers modelDic:result];
        [self.tableViewOfQuestionsAndAnswers reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建子控件
- (void)createSubView{
    // 创建 tableView
    self.tableViewOfQuestionsAndAnswers = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.bounds.size.height - 64 - 49) style:UITableViewStylePlain];
    self.tableViewOfQuestionsAndAnswers.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    [self.view addSubview:self.tableViewOfQuestionsAndAnswers];
    self.tableViewOfQuestionsAndAnswers.delegate = self;
    self.tableViewOfQuestionsAndAnswers.dataSource = self;
    [self.tableViewOfQuestionsAndAnswers registerNib:[UINib nibWithNibName:@"TVCellOfQuestionsAndAnswers" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfQuestionsAndAnswers"];
    // 上拉刷新
    self.tableViewOfQuestionsAndAnswers.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 创建上面的广告轮播图
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 3 / 8)];
    BannerCycleImageView *bannerCycleImageView = [[BannerCycleImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2 / 8)];
    bannerCycleImageView.backgroundColor = [UIColor redColor];
    [tableHeaderView addSubview:bannerCycleImageView];
    // 创建下面的可以点击的 CollectView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 8);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    BaseCollectionView *choiceButtonCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, bannerCycleImageView.bounds.size.height, SCREEN_WIDTH, SCREEN_HEIGHT / 8) collectionViewLayout:layout];
    choiceButtonCollectionView.backgroundColor = [UIColor redColor];
    [tableHeaderView addSubview:choiceButtonCollectionView];
    self.tableViewOfQuestionsAndAnswers.tableHeaderView = tableHeaderView;
    choiceButtonCollectionView.delegate = self;
    choiceButtonCollectionView.dataSource = self;
    [choiceButtonCollectionView registerNib:[UINib nibWithNibName:@"CVCellOfChoiceButton" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfChoiceButton"];
}
#pragma mark - 上拉刷新
- (void)loadNewData{
    [self handleData];
    [self.tableViewOfQuestionsAndAnswers reloadData];
    [self.tableViewOfQuestionsAndAnswers.mj_header endRefreshing];
    
}
#pragma mark - CollectionView 代理方法 item 数量的确定
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
#pragma mark - collectionView 代理方法 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfChoiceButton *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfChoiceButton" forIndexPath:indexPath];
    if (indexPath.row == 1) {
        cell.myImageView.image = [UIImage imageNamed:@"QuestionsAndAnswers_64"];
        cell.titleLabel.text = @"问答精选集";
        cell.purposeLabel.text = @"各类问题的精选回答";
    }
    return cell;
}
#pragma mark - collectionView cell 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"dfasdfdsfdsafdsa");
    if (indexPath.row == 0) {
        VCOfSpecialistDisabuse *vcOfSpecialistDisabuse = [[VCOfSpecialistDisabuse alloc] init];
        [self.navigationController pushViewController:vcOfSpecialistDisabuse animated:YES];
    } else {
        VCOfQuestionsAndAnswersAuslese *vcOfQuestionsAndAnswersAuslese = [[VCOfQuestionsAndAnswersAuslese alloc] init];
        [self.navigationController pushViewController:vcOfQuestionsAndAnswersAuslese animated:YES];
    }
}
#pragma mark - tableView 代理方法 row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rootClassOfQuestionsAndAnswers.data.question.count;
}
#pragma mark - tableView 代理方法 row 的高度的确定
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT * 0.50;
}
#pragma mark - tableView 的代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfQuestionsAndAnswers *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfQuestionsAndAnswers" forIndexPath:indexPath];
    cell.nicknameLabel.text = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].user.nickname;
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfQuestionsAndAnswers.data.question[indexPath.row].user.avatar] placeholderImage:[UIImage imageNamed:@""]];
    cell.avatarImageView.layer.cornerRadius = 10;
    cell.avatarImageView.clipsToBounds = YES;
    cell.publishLabel.text = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].publish;
    cell.comment_numLabel.text = [NSString stringWithFormat:@"%ld",_rootClassOfQuestionsAndAnswers.data.question[indexPath.row].comment_num];
    NSMutableString *singleStringName = [NSMutableString string];
    NSMutableString *finalStringName = [NSMutableString string];
    for (TagsOfQuestion *tagsOfQuestion in _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].tags ) {
        singleStringName = [NSMutableString stringWithFormat:@"%@", tagsOfQuestion.name];
        finalStringName = [NSMutableString stringWithFormat:@"%@ %@", finalStringName, singleStringName];
    }
    cell.tagsLabel.text = finalStringName;
    if ([finalStringName isEqualToString:@""]) {
        cell.tagsImageView.image = [UIImage imageNamed:@""];
    } else {
        cell.tagsImageView.image = [UIImage imageNamed:@"Tags_16"];
    }
    cell.nicknameOfAnswerLabel.text = [NSString stringWithFormat:@"%@ 回答", _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].answer.nickname];
    cell.contentLabel.text = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].content;
    cell.titleLabel.text = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].title;
    cell.contentOfAnswerLabel.text = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].answer.content;
    return cell;
}
#pragma mark - 分区头自定义 View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH - 20, 44)];
    [view addSubview:label];
    view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    label.text = @"全部分类";
    return view;
}
#pragma mark - 分区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
#pragma mark - tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld", indexPath.row);
    VCOfQAADetailedInformation *vcOfQAADetailedInformation = [[VCOfQAADetailedInformation alloc] init];
//    _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].question_id
    vcOfQAADetailedInformation.question_id = _rootClassOfQuestionsAndAnswers.data.question[indexPath.row].question_id;
    [self.navigationController pushViewController:vcOfQAADetailedInformation animated:YES];
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
